package com.example.day04_fragment;

import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.RemoteViews;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.NotificationTarget;

import java.util.HashMap;

public class TwoFragment extends Fragment implements MyLisenter {

    private TextView fg2Tv;
    private ListView lv;

    private MusicAdapter musicAdapter;

    public TwoFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_two, container, false);
        initView(view);

        musicAdapter = new MusicAdapter();
        lv.setAdapter(musicAdapter);
        HttpUtils.doGet("http://10.161.9.80:7004/song/json",new HashMap<String,String>(){{put("singer","邓紫棋");}},MusicEntity.class, r->{
            musicAdapter.addAll(r.getData());
        },t->{t.printStackTrace();});

        //点击item,弹出菜单，删除，详情，通知
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showMenu(view,position);
            }
        });
        //长按item,弹出窗体
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                shopWindow(position);
                return true;
            }
        });

        return view;
    }
    //popupwindow回顾
    private void shopWindow(int position) {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.item_music, null);
        TextView tv = inflate.findViewById(R.id.tv);
        ImageView iv = inflate.findViewById(R.id.iv);
        tv.setText(musicAdapter.getItem(position).getName()+"");
        Glide.with(getContext()).load(musicAdapter.getItem(position).getPicUrl()).into(iv);
        PopupWindow popupWindow = new PopupWindow();
        popupWindow.setHeight(300);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setContentView(inflate);
        popupWindow.setOutsideTouchable(true);
        //添加背景虚化
        WindowManager.LayoutParams attributes = getActivity().getWindow().getAttributes();
        attributes.alpha = 0.5f;
        getActivity().getWindow().setAttributes(attributes);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams attributes = getActivity().getWindow().getAttributes();
                attributes.alpha =1f;
                getActivity().getWindow().setAttributes(attributes);
            }
        });
        popupWindow.setBackgroundDrawable(getContext().getDrawable(R.drawable.bg));
        popupWindow.showAtLocation(lv, Gravity.BOTTOM,0,0);

    }

    private void showMenu(View view, int position) {
        PopupMenu popupMenu = new PopupMenu(getContext(), view);
        popupMenu.inflate(R.menu.menu);
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.shanchu:
                        shanchu(position);
                        break;
                    case R.id.xiangqing:
                        shangxiang(position);
                        break;
                    case R.id.tongzhi:
                        tongzhi(position);
                        break;
                }
                return false;
            }
        });
        popupMenu.show();
    }
    //自定义通知
    private void tongzhi(int position) {
        RemoteViews views = new RemoteViews(getContext().getPackageName(), R.layout.item_music);
        views.setTextViewText(R.id.tv,musicAdapter.getItem(position).getName());
        Intent intent = new Intent(getContext(), MainActivity2.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 201, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationManager manager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(getContext())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setCustomContentView(views)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            manager.createNotificationChannel(new NotificationChannel("id","name",NotificationManager.IMPORTANCE_DEFAULT));
            builder.setChannelId("id");
        }
        Notification notification = builder.build();
        manager.notify(1,notification);
        NotificationTarget target = new NotificationTarget(getContext(), R.id.iv, views, notification, 1);
        Glide.with(getContext())
                .asBitmap()
                .load(musicAdapter.getItem(position).getPicUrl())
                .into(target);

    }

    //自定义对话框
    AlertDialog dialog;
    private void shangxiang(int position) {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.diy_dialog, null);
        TextView diyTv = inflate.findViewById(R.id.diy_tv);
        ImageView diyIv = inflate.findViewById(R.id.diy_iv);
        Button diyBtn = inflate.findViewById(R.id.diy_btn);
        diyTv.setText(musicAdapter.getItem(position).getName()+"");
        Glide.with(getContext()).load(musicAdapter.getItem(position).getPicUrl()).into(diyIv);
        diyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        //自定义对话框
        dialog = new AlertDialog.Builder(getContext())
                .setView(inflate)
                .create();
        dialog.show();
    }

    private void shanchu(int position) {
        //弹出对话框
        new AlertDialog.Builder(getContext())
                .setTitle("提示")
                .setMessage("确定删除吗")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        musicAdapter.remove(position);
                    }
                })
                .show();
    }

    @Override
    public void setData(Object o) {
        fg2Tv.setText(o.toString() + "");
    }

    private void initView(View view) {
        fg2Tv = (TextView) view.findViewById(R.id.fg2_tv);
        lv = (ListView) view.findViewById(R.id.lv);
    }
}